<template>
  <div>
    <el-autocomplete
      placeholder="请输入姓名或职位"
      v-model="choosedp"
      :fetchSuggestions="searchjbr"
      @select="selectjbr"
      style="width:100%;"
      clearable
      @clear="clearchoosedp"
      :disabled="disabled"
    ></el-autocomplete>
  </div>
</template>

<script>
export default {
  props: {
    itempersonid: String,
    disabled: Boolean,
  },
  data() {
    return {
      ts: this.newStore(),
      jbrList: [],
      choosedp: '',
    };
  },
  created() {
    this.getjbrList();
  },
  methods: {
    clearchoosedp() {
      this.$emit('getpersonid', { id: '', value: '' });
    },
    // 获取经办人列表
    getjbrList() {
      this.jbrList = [];
      const req = {
        handle:
          '45EE8FEFE87870C53A0536D8566DA8C94345851CDDF60960CBCA139672EA5B8C230BE336D992C9E341DC33B3A5E90841',
        query: '',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        for (let index = 0; index < ts.d.data.operation1.length; index += 1) {
          const a = {};
          a.value = `${ts.d.data.operation1[index].hash.username} - ${ts.d.data.operation1[index].hash.orgname}`;
          a.id = ts.d.data.operation1[index].hash.pid;
          this.jbrList.push(a);
        }
        for (let i = 0; i < this.jbrList.length; i += 1) {
          if (this.itempersonid) {
            if (this.itempersonid == this.jbrList[i].id) {
              this.choosedp = this.jbrList[i].value;
            }
          }
        }
      });
    },
    // 搜索经办人
    searchjbr(queryString, cb) {
      const results = queryString
        ? this.jbrList.filter(this.createStateFilter(queryString))
        : this.jbrList;

      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return jbrList => jbrList.value.indexOf(queryString) > -1;
    },
    // 选择经办人后
    selectjbr(val) {
      console.log(val);
      this.$emit('getpersonid', val);
    },
  },
};
</script>

<style>
</style>
